package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import java.util.Scanner;

/*
学生 用户交互类
键盘录入
输出语句

增
删
改
查
 */
public abstract class BaseStudentController {
    // 调用Service 的新增方法
    private StudentService studentService = new StudentService();
     Scanner sc = new Scanner(System.in);

    // 学生系统的入口
    public final void start() {

        //死循环
        loop:
        while (true) {
            //键盘录入
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudents();
                    break;
                case "5":
                    System.out.println("回到主菜单");
                    //break loop;
                    return;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    public  final  void updateStudent() {
        Student[] students = studentService.findAllStudents();
        if (students == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String sid = getSid();
        // 继续录入其他信息
        Student student = getStudentInfo(sid);
        studentService.updateStudent(sid,student);
        System.out.println("修改成功");
    }



    public  final String getSid() {
        //录入学号
        System.out.println("请输入学号");
        String sid=null;
        while (true) {
            sid= sc.next();
            //判断学号是否存在
            boolean exists = studentService.isExists(sid);
            //存在
            if (exists) {
                break;
            } else { //不存在 重新录入
                System.out.println("不存在,请重新录入");
            }
        }
        return sid;
    }

    public  final void deleteStudent() {
        Student[] students = studentService.findAllStudents();
        if (students == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String sid = getSid();
        //删除
        studentService.deleteStudent(sid);
        System.out.println("删除成功");

    }

    public final  void findAllStudents() {
        //调用service findALl
        Student[] students = studentService.findAllStudents();
        if (students == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null) {
                System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge()
                        + "岁\t" + student.getBirthday());
            }
        }


    }

    public  final void addStudent() {

        //键盘录入各种信息

        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //调用service 方法 来 判断学号是否存在
            boolean isExists = studentService.isExists(sid);
            if (isExists) {
                System.out.println("已经存在请重新输入");
            } else {
                //不存在
                break;
            }
        }
        Student student = getStudentInfo(sid);

        boolean flag = studentService.addStudent(student);//如果数组满了 可能放不下 所以 需要一个返回值 返回插入成功还是失败
        if (flag) {
            System.out.println("新增成功");
        } else {
            System.out.println("添加失败");
        }

    }


    public abstract Student getStudentInfo(String sid);
}
